package kibahed.schnapsen;

import java.awt.GridLayout;
import java.awt.Image;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import kibahed.interfaces.cards.Card;
import kibahed.interfaces.cards.CardFactory;
import kibahed.interfaces.cards.ECardValue;
import kibahed.interfaces.cards.EGame;
import kibahed.interfaces.cards.ESuite;

public class Bildkarte extends JLabel {

	private URL path = null;
	ImageIcon icon = null;
	ImageIcon scaledIcon = null;
	boolean isHidden = false;
	boolean hasChanged = false;
	Card card;
	private static int cardWidth = 59;
	private static int cardHeight = 91;
	private static double globalMultipier = 2;
	private double sizeMultipier = 1;
	private static List<Bildkarte> alleKarten = new LinkedList<Bildkarte>();

	public static void main(String[] args) {

		JFrame frame = new JFrame();

		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(0, 5));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		for (Card card : CardFactory.getCardDeck(EGame.SCHNAPSEN)) {
			Bildkarte bk = new Bildkarte(card);
			JLabel label = new JLabel(bk.getImage());
			panel.add(label);
		}
		frame.add(panel);
		frame.pack();

		frame.setVisible(true);

	}

	public Bildkarte(Card card) {
		super();
		this.card = card;
		this.setIcon(getImage());
		synchronized (alleKarten) {
			synchronized (this) {
				alleKarten.add(this);
			}
		}
	}

	public Bildkarte() {
		super();
		synchronized (alleKarten) {
			synchronized (this) {
				alleKarten.add(this);
			}
		}
	}

	private ImageIcon getImage() {

		try {
			// System.out.println("path=" + path);
			if (icon == null || hasChanged) {
				if (isHidden) {
					icon = new ImageIcon(String.format("cards/%s.%s", "BACK",
							"png"));
				} else {
					icon = new ImageIcon(String.format("cards/%s.%s",
							card.getTag(""), "png"));
				}
				hasChanged = false;
			}
			scaledIcon = new ImageIcon(icon.getImage().getScaledInstance(
					getCardWidth(), getCardHeight(), Image.SCALE_FAST));
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return scaledIcon;
	}

	public int getCardWidth() {
		return (int) (cardWidth * globalMultipier * sizeMultipier);
	}

	public int getCardHeight() {
		return (int) (cardHeight * globalMultipier * sizeMultipier);
	}

	public static double getGlobalMultipier() {
		return globalMultipier;
	}

	public static void setGlobalMultipier(double d) {
		Bildkarte.globalMultipier = d;
	}

	public double getSizeMultipier() {
		return sizeMultipier;
	}

	public void setSizeMultipier(double sizeMultipier) {
		this.sizeMultipier = sizeMultipier;
	}

	public Card getCard() {
		return card;
	}

	public void setCard(Card card) {
		this.card = card;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((card == null) ? 0 : card.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Bildkarte other = (Bildkarte) obj;
		if (card == null) {
			if (other.card != null)
				return false;
		} else if (!card.equals(other.card))
			return false;
		return true;
	}

	public static Bildkarte getUmgedrehteKarte() {
		Card card = new Card();
		Bildkarte karte = new Bildkarte(card);
		karte.hasChanged = true;
		karte.isHidden = true;
		karte.updateImage();
		return karte;
	}

	public void updateImage() {

		this.setIcon(getImage());

	}

	public static void updateSizeAllCards() {
		synchronized (alleKarten) {
			for (Bildkarte karte : alleKarten) {
				synchronized (karte) {
					if (karte != null) {
						karte.updateImage();
					}
				}
			}
		}
	}

	public boolean isHidden() {
		return isHidden;
	}

	public void setHidden(boolean isHidden) {
		hasChanged=true;
		this.isHidden = isHidden;
	}
	
	public static Bildkarte getBildkarte(String string) {
		try {
			String[] values = string.split("/");
			String suit = values[0];
			String cardValue = values[1];
			ESuite eSuite = ESuite.convert(suit);
			ECardValue eValue = ECardValue.convert(cardValue);
			if (eSuite != null && eValue != null) {
				Card card = CardFactory
						.getCard(EGame.SCHNAPSEN, eSuite, eValue);
				return new Bildkarte(card);
			}
		} catch (Exception ex) {
			System.out.println("unable to find card: " + string);
			ex.printStackTrace();
		}
		return null;

	}
	

}
